using System;
using EPiServer.Common;
using EPiServer.Common.Categories;

namespace EPiAbstractions.Common.Categories
{
    public class CategoryHandlerFacade : ICategoryHandlerFacade
    {
        private static CategoryHandlerFacade _instance;

        public CategoryHandlerFacade()
        {
            CategoryHandler.CategoryAdded += OnCategoryAdded;
            CategoryHandler.CategoryUpdated += OnCategoryUpdated;
            CategoryHandler.CategoryRemoved += OnCategoryRemoved;
        }

        public static CategoryHandlerFacade Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new CategoryHandlerFacade();

                return _instance;
            }

            set { _instance = value; }
        }

        #region ICategoryHandlerFacade Members

        public event EPiServerCommonEventHandler CategoryAdded;
        public event EPiServerCommonEventHandler CategoryUpdated;
        public event EPiServerCommonEventHandler CategoryRemoved;

        public virtual ICategory AddCategory(ICategory category)
        {
            return CategoryHandler.AddCategory(category);
        }

        public virtual void UpdateCategory(ICategory category)
        {
            CategoryHandler.UpdateCategory(category);
        }

        public virtual void RemoveCategory(ICategory category)
        {
            CategoryHandler.RemoveCategory(category);
        }

        public virtual ICategory GetCategory(Int32 id)
        {
            return CategoryHandler.GetCategory(id);
        }

        public virtual ICategory GetCategory(String path)
        {
            return CategoryHandler.GetCategory(path);
        }

        public virtual ICategory GetCategory(ISite site, String path)
        {
            return CategoryHandler.GetCategory(site, path);
        }

        public virtual CategoryCollection GetCategories()
        {
            return CategoryHandler.GetCategories();
        }

        public virtual CategoryCollection GetCategories(ISite site)
        {
            return CategoryHandler.GetCategories(site);
        }

        public virtual CategoryCollection GetCategories(ICategory parentCategory)
        {
            return CategoryHandler.GetCategories(parentCategory);
        }

        public virtual CategoryCollection GetEntityCategories(ICategorizableEntity categorizableEntity)
        {
            return CategoryHandler.GetEntityCategories(categorizableEntity);
        }

        public virtual CategorizableEntityCollection GetCategorizedItems(ICategory category, Int32 page, Int32 pageSize,
                                                                         out Int32 totalItems)
        {
            return CategoryHandler.GetCategorizedItems(category, page, pageSize, out totalItems);
        }

        public virtual CategorizableEntityCollection GetCategorizedItems(Type type, ICategory category, Int32 page, Int32 pageSize,
                                                                         out Int32 totalItems)
        {
            return CategoryHandler.GetCategorizedItems(type, category, page, pageSize, out totalItems);
        }

        public virtual CategorizableEntityCollection GetCategorizedItems(CategoryCollection categories, Int32 page, Int32 pageSize,
                                                                         out Int32 totalItems)
        {
            return CategoryHandler.GetCategorizedItems(categories, page, pageSize, out totalItems);
        }

        public virtual CategorizableEntityCollection GetCategorizedItems(Type type, CategoryCollection categories, Int32 page,
                                                                         Int32 pageSize, out Int32 totalItems)
        {
            return CategoryHandler.GetCategorizedItems(type, categories, page, pageSize, out totalItems);
        }

        #endregion

        public virtual void OnCategoryAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (CategoryAdded != null)
                CategoryAdded(sender, e);
        }

        public virtual void OnCategoryUpdated(String sender, EPiServerCommonEventArgs e)
        {
            if (CategoryUpdated != null)
                CategoryUpdated(sender, e);
        }

        public virtual void OnCategoryRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (CategoryRemoved != null)
                CategoryRemoved(sender, e);
        }
    }
}